// pages/wordList/wordList.js
const db = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    word: {
      english: 'apple',
      chinese: '苹果',
      pro: '[proc]'
    },
    list: [],
    isShowConfirm: false,
    myindex: ''
  },
  inputValue: function (e) {
    this.setData({
      [`${e.currentTarget.dataset.gater}`]: e.detail.value
    })
  },
  modifyWord: function (e) {

    let myindex = e.target.dataset.myindex
    this.setData({
      myindex: myindex
    })
    let word = e.target.dataset.item
    this.setData({
      isShowConfirm: true,
      word: word
    })
  },
  cancel: function () {
    var that = this
    that.setData({
      isShowConfirm: false,
    })
  },
  //删除
  delWord:function(e){

    wx.showModal({
      title: '提示',
      content: '确认删除吗?',
      success: res => {
          if (res.confirm) {
              console.log(res.confirm)
              let myindex = e.target.dataset.myindex
              let word = e.target.dataset.item
              var wordlist = this.data.list
              wordlist.splice(myindex,1)
              this.setData({
                list:wordlist
              })
              //删除数据库中的单词
              db.collection("words").doc(word._id).remove({
                success:function(res){
                  wx.showToast({
                    title: '删除成功',
                  })
                }
              })
          } else {
              console.log(res.confirm)
              return;
          }
      }
  })

   

  },
  confirmAcceptance: function () {
    var that = this
    var wordlist = this.data.list
    //保存单词
     console.log(this.data.list[this.data.myindex].english)
     wordlist[this.data.myindex].english = this.data.word.english
     wordlist[this.data.myindex].chinese = this.data.word.chinese
     wordlist[this.data.myindex].proc = this.data.word.proc
    that.setData({
      isShowConfirm: false,
      list:wordlist
    }),
    //保存单词数据到数据库
    db.collection('words').doc(this.data.word._id).update({
      data:{
        chinese:this.data.word.chinese,
        english:this.data.word.english,
        proc:this.data.word.proc
      },
      success:function(res){
        wx.showToast({
          title: '保存成功',
        })
        console.log(res)
      }
    })
  },
  getAll: function (e) {

    db.collection('words').skip(this.data.list.length).limit(20).get().then(res => {
      console.log(res)
      this.setData({
        list: this.data.list.concat(res.data)
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getAll()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    console.log("onReachBottom")
    this.getAll()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})